package Array;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhang lei
 * @Date 2021-11-17 13:19
 */
public class Algo_Offer10 {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        System.out.println(subArraySum(nums, 3));
    }

    /**
     * 一个数组中，和为k的连续子数组个数
     * @param nums
     * @param k
     * @return
     */
    public static int subArraySum(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0,1);
        int count = 0;
        int sum = 0;
        for (int num : nums) {
            sum += num;
            count += map.getOrDefault(sum-k, 0);
            map.put(sum, map.getOrDefault(sum, 0)+1);
            System.out.println(num +":sum"+sum+",count:"+count+",map"+map.toString());
        }
        return count;
    }
}
